package com.tongjie.hotelbooking.controller.common;

import com.tongjie.hotelbooking.constrants.ErrorCode;
import com.tongjie.hotelbooking.http.BaseResponse;
import com.tongjie.hotelbooking.sms.service.SmsService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sms")
public class SmsController {

    @Resource
    private SmsService smsService;

    @PostMapping("/send")
    public BaseResponse sendSmsCode(@RequestBody SendSmsCodeRequest request) {
        if (request.getMobile() == null || request.getMobile().length() != 11) {
            return BaseResponse.fail(ErrorCode.SmsMobileInvalid, "mobile invalid", null);
        }
        if (!this.smsService.sendSmsCode(request.getMobile())) {
            return BaseResponse.fail(ErrorCode.SmsCodeExists, "sms code exists", null);
        }
        return BaseResponse.success(null);
    }

    @Data
    private static class SendSmsCodeRequest {
        private String mobile;
    }

}
